<?php

namespace Org\Util;

use Com\Email\PHPMailer;

/**
 * 邮件发送发送工具类
 * @modify trainyao 2015-09-27 创建
 */
class Email {

    /**
     * 邮件服务器IP
     * @var string
     * @access private
     */
    private $host = 'smtp.qiye.163.com';

    /**
     * 邮件服务器发送端口
     * @var integer
     * @access private
     */
    private $port = 994;

    /**
     * 邮件发送安全层
     * @var string
     * @access private
     */
    private $secure = "ssl";

    /**
     * 邮件服务器登录邮箱
     * @var string
     * @access private
     */
    private $username = 'admin@uclbrt.com';

    /**
     * 邮件服务器登录密码
     * @var string
     * @access private
     */
    private $password = 'qrlock8333153BRT';

    /**
     * 设置邮件显示来自此邮箱
     * @var string
     * @access private
     */
    private $fromAddress = 'admin@uclbrt.com';

    /**
     * 设置邮件显示来自此名称
     * @var string
     * @access private
     */
    private $fromName = '畅联科技';

    /**
     * 设置邮件回复到此邮箱
     * @var string
     * @access private
     */
    private $replyAddress = 'admin@uclbrt.com';

    /**
     * 设置邮件回复到此名称
     * @var string
     * @access private
     */
    private $replyName = '畅联科技';

    /**
     * 提示内容
     * @var string
     * @access private
     */
    private $altBody = 'To view the message, please use an HTML compatible email viewer!';

    /**
     * 构造函数
     * 创建一个邮件发送对象
     * @modify trainyao 2015-09-27 创建
     * @param string $host  邮件服务器IP
     * @param integer $port  邮件服务器发送端口
     * @param string $secure  邮件发送安全层
     * @param string $username  邮件服务器登录邮箱
     * @param string $password  邮件服务器登录密码
     * @param string $fromAddress  设置邮件显示来自此邮箱
     * @param string $fromName  设置邮件显示来自此名称
     * @param string $replyAddress  设置邮件回复到此邮箱
     * @param string $replyName  设置邮件回复到此名称
     * @param string $altBody  提示内容
     */
    public function __construct($host = NULL, $port = NULL, $secure = NULL, $username = NULL, $password = NULL, $fromAddress = NULL, $fromName = NULL, $replyAddress = NULL, $replyName = NULL,
            $altBody = NULL) {
        if (!empty($host)) {
            $this->host = $host;
        }
        if (!empty($port)) {
            $this->port = $port;
        }
        if (!empty($secure)) {
            $this->secure = $secure;
        }
        if (!empty($username)) {
            $this->username = $username;
        }
        if (!empty($password)) {
            $this->password = $password;
        }
        if (!empty($fromAddress)) {
            $this->fromAddress = $fromAddress;
        }
        if (!empty($fromName)) {
            $this->fromName = $fromName;
        }
        if (!empty($replyAddress)) {
            $this->replyAddress = $replyAddress;
        }
        if (!empty($replyName)) {
            $this->replyName = $replyName;
        }
        if (!empty($altBody)) {
            $this->altBody = $altBody;
        }
    }

    /**
     * 邮件发送函数
     * 创建一个邮件发送对象
     * @modify trainyao 2015-09-27 创建
     * @param string $to  发送到此邮箱
     * @param string $subject  邮件主题
     * @param string $body  邮件内容
     * @return  mixed 邮件发送结果
     */
    public function postMail($to, $subject = '', $body = '') {
        date_default_timezone_set('Asia/Shanghai');
        $mail = new PHPMailer();
        $body = preg_replace("/\\\\/", '', $body);
        $mail->CharSet = "utf-8";
        $mail->IsSMTP();
        $mail->SMTPDebug = 0;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = $this->secure;
        $mail->Host = $this->host;
        $mail->Port = $this->port;
        $mail->Username = $this->username;
        $mail->Password = $this->password;
        $mail->SetFrom($this->fromAddress, $this->fromName);
        $mail->AddReplyTo($this->replyAddress, $this->replyName);
        $mail->Subject = $subject;
        $mail->AltBody = $this->altBody;
        $mail->MsgHTML($body);
        $address = $to;
        $mail->AddAddress($address, '');
        // if( !$mail->Send() ){
        //     echo 'Mailer Error: ' . $mail->ErrorInfo;exit;
        // }
        return $mail->Send();
    }

    /**
     * 客栈申请邮件发送信息提示
     * @modify songanwei 2016-02-03
     * @param email $originEmail  接收邮件
     * @param string $hotelName 客栈名称
     * @param string $mobile 手机号
     * @param string $address 地址
     * @param string $applyName 申请人
     * @param string $from 消息来源
     * @return type
     */
    public function applyHotel($originEmail, $hotelName, $mobile, $address, $applyName, $from) {
        $title = '美住 - 客栈申请 -' . $hotelName;
        $html = '客栈申请邮件提示，申请信息如下：<br />客栈名称:' . $hotelName . '<br />手机号：' . $mobile . '<br/>联系人:' . $applyName . '<br />地址：' . $address.'<br />消息来源：' . $from;
        return $this->postMail($originEmail, $title, $html);
    }
    /**
     * 经销商申请邮件发送信息提示
     * @modify songanwei 2016-02-03
     * @param email $originEmail  接收邮件
     * @param string $hotelName 客栈名称
     * @param string $mobile 手机号
     * @param string $address 地址
     * @param string $applyName 申请人
     * @return type
     */
    public function applyDealer($originEmail, $name, $mobile) {
        $title = '美住 - 经销商申请 -' . $name;
        $html = '经销商申请邮件提示，申请信息如下：<br />联系人:' . $name . '<br />手机号：' . $mobile;
        return $this->postMail($originEmail, $title, $html);
    }
    /**
     * 经销商客栈申请邮件发送信息提示
     * @modify zhangzhen 2017-01-04
     * @param email $originEmail  接收邮件
     * @param string $hotelName 客栈名称
     * @param string $mobile 手机号
     * @param string $address 地址
     * @param string $applyName 申请人
     * @return type
     */
    public function dealerApplyHotel($originEmail, $hotelName, $mobile, $address, $applyName) {
        $title = '美住 - 经销商客栈申请 -' . $hotelName;
        $html = '客栈申请邮件提示，申请信息如下：<br />客栈名称:' . $hotelName . '<br />手机号：' . $mobile . '<br/>联系人:' . $applyName . '<br />地址：' . $address;
        return $this->postMail($originEmail, $title, $html);
    }
    /**
     * 分享房卡
     * @modify zhangzhen 2017-1-18 创建
     * @param  string $email     目标邮箱
     * @param  string $content   正文
     * @param  string $lang      语言选择
     */
    public function shareCard($email, $content){
        $title = '【畅联科技】 分享钥匙';
        return $this->postMail($email, $title, $content);
    }
}

?>